import loginApi from '@/api/auth/loginApi'
import userCenterApi from '@/api/sys/userCenterApi'
import dictApi from '@/api/dev/dictApi'
import approvalApi from '@/api/biz/approvalApi'
import appinfoflowApi from '@/api/biz/appinfoflowApi'
import router from '@/router'
import tool from '@/utils/tool'
import { message } from 'ant-design-vue'
import sysConfig from '@/config/index'


// 过滤待办参数
const setTodoParams = (appType) => {
	const config = sysConfig.DEVICE_CONFIG
	let arr = []
	config.forEach((v) => {
		if (appType.includes(v.key)) {
			arr.push(v.oid)
		}
	})
	return arr.join()
}



// 获取待办待阅的参数并跳转
const getTodoParams = async () => {
	const para = tool.data.get('TODO_PARAMS')

	// 0待办已办  1待阅已阅
	const dbType = para.sendId ? 1 : 0
	const params = {
		messageTrackId: para.message_track_id,
		sendId: para.sendId,
		dbType
	}
	const data = await approvalApi.getParams(params)
	if (data) {
		const query = {
			id: data.messageTrackId,
			approvalType: data.zt,
			originatorName: data.originatorName,
			processInstanceId: data.processId,
			tableName: setTodoParams(data?.appType),
			title: data?.pendingName
		}
		// 待办
		if (data?.doneStatus == 0) {
			query.doneStatus = '0'
			query.stepId = data?.stepId
		}
		// 已办
		if (data?.doneStatus == 1) {
			query.doneStatus = '1'
		}
		// 待阅
		if (data?.readState == 0) {
			query.doneStatus = '2'
			query.wfSendRoundInfoId = data?.wfSendRoundInfoId
		}
		// 已阅
		if (data?.readState == 1) {
			query.doneStatus = '3'
		}
		if (data.stepId === 'a001' && (data.zt === '6' || data.zt === '7')) {
			// 这个是测试应用的新增或者编辑被退回了
			router.push({
				path: '/biz/appinfo/testForm',
				query: {
					type: '2',
					messageTrackId: data.messageTrackId
				}
			})
		} else {
			router.replace({
				path: '/biz/approval/detail',
				query: query
			})
		}

	}
}

export const afterLogin = async (loginToken, istodo) => {
	tool.data.set('TOKEN', loginToken)
	// 获取登录的用户信息
	const loginUser = await loginApi.getLoginUser()
	tool.data.set('USER_INFO', loginUser)

	// 获取用户的菜单
	// let menu = await userCenterApi.userLoginMenu()
	let menuls = await userCenterApi.userLoginMenu()
	const menu = menuls.reverse()
	// const indexMenu = menu[0].children[0].path
	const indexMenu = '/biz/approval/todo'
	tool.data.set('MENU', menu)
	// 重置系统默认应用
	tool.data.set('SNOWY_MENU_MODULE_ID', menu[0].id)

	dictApi.dictTree().then((data) => {
		// 设置字典到store中
		tool.data.set('DICT_TYPE_TREE_DATA', data)
	})
	let fileList = await appinfoflowApi.queryTemplateUrl()
	tool.data.set('FLIE_LIST_UPLOAD', JSON.stringify(fileList))
	if (!istodo) {
		// 存储是否是待办进入 1是  0否
		tool.session.set('IS_TODO', '0')
		// 正常的登录
		message.success('登录成功')
		router.replace({
			path: indexMenu
		})
	} else {
		// 待办待阅的登录
		getTodoParams()
		// 存储是否是待办进入 1是  0否
		tool.session.set('IS_TODO', '1')
	}
}

export const param2Obj = () => {
	const url = window.location.href
	const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
	if (!search) {
		return {}
	}
	const obj = {}
	const searchArr = search.split('&')
	searchArr.forEach((v) => {
		const index = v.indexOf('=')
		if (index !== -1) {
			const name = v.substring(0, index)
			const val = v.substring(index + 1, v.length)
			obj[name] = val
		}
	})
	return obj
}
// 存储待办id等参数
export const setTrackId = (param) => {
	tool.data.set('TODO_PARAMS', param)
}
// 清理缓存
export const clear = () => {
	tool.data.clear()
}
